<?php
/******************************************************************************
 *
 *    OpenZoowa - Web App to manage events
 *    Copyright (C) 2008 Jesus Ruiz-Ayucar
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/
   if (!defined('ZOOWA')) {
      include_once('../../config.php');
   }
   class Category {
      private $id;
      private $category_parent;
      private $name;
      private $name_clean;

      public function __construct($id = null) {
         if (isset($id) && Util::is_numeric($id) && $id > 0) {
            global $db, $lang;
            $SQL  = 'SELECT * FROM '.$GLOBALS["db_pre"].'categories WHERE ';
            $SQL .= 'id = '.$db->escape($id);
            $object = $db->get_row($SQL);
            $this->id = $object->id;
            $pid = $object->category_parent_id;
            $id = $object->id;
            $this->name = $lang['categories'][$pid][$id][0];
            $this->name_clean = $lang['categories'][$pid][$id][1];
         }
      }

      public function getId() {
         return $this->id;
      }

      public function getParent() {
         return $this->category_parent;
      }

      public function getName() {
         return $this->name;
      }

      public function getNameClean() {
         return $this->name_clean;
      }

      public function __toString() {
         return $this->name;
      }

      public static function getList($parent = 0) {
         global $db;

         $SQL  = 'SELECT id ';
         $SQL .= 'FROM '.$GLOBALS["db_pre"].'categories WHERE ';
         $SQL .= 'category_parent_id = ' . $db->escape($parent);
         foreach($db->get_col($SQL) as $cat) {
            $categories[] = new Category($cat);
         }
         return $categories;
      }

      public static function getIdFromClean($name) {
         global $lang;

         foreach($lang['category'] as $parent) {
            foreach ($parent as $id => $cat) {
               if ($cat[1] == $name) {
                  return $id;
               }
            }
         }
      }
   }
?>